package com.codebattery.controller;

import com.codebattery.ApiJsonObject;
import com.codebattery.ApiJsonProperty;
import com.codebattery.domain.entity.Member;
import com.codebattery.domain.entity.MemberReceiveAddress;
import com.codebattery.domain.model.SimpleFilterResolver;
import com.codebattery.domain.model.UserLoginInfo;
import com.codebattery.service.MemberReceiveAddressService;
import com.codebattery.service.MemberService;
import io.lettuce.core.GeoArgs;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import static org.springframework.data.jpa.domain.Specification.where;


@RestController
@RequestMapping("/member-receive-address")
@Slf4j
public class AddressController {
    @Resource
    private MemberReceiveAddressService memberReceiveAddressService;

    @ApiOperation(value="模板示例",notes="")
    @RequestMapping(value = "/test", method = RequestMethod.GET, produces = "application/json")
    public String test(Member member) throws Exception {


        return null;
    }

    @ApiOperation(value="获取用户收货地址",notes="")
    @RequestMapping(value = "/get", method = RequestMethod.POST, produces = "application/json")
    public MemberReceiveAddress getReceiveAddress(Long addressId) throws Exception {
        log.info("获取用户收货地址");

        MemberReceiveAddress memberAddress = memberReceiveAddressService.get(addressId);

        return memberAddress;
    }

    @ApiOperation(value="用户收货地址列表",notes="")
    @RequestMapping(value = "/", method = RequestMethod.POST, produces = "application/json")
    public Page<MemberReceiveAddress> list(
                                @ApiJsonObject(name = "MemberReceiveAddress-list-param", value = {
                                @ApiJsonProperty(key = "username", example = "322", description = "用户名", type = "string", required = true)})
                        @RequestBody Map param) throws Exception {
        log.info("用户已经登录");


        Pageable pageable = PageRequest.of(0, 10, Sort.unsorted());

        String memberId = (String)param.get("username").toString();
        Page memberAddress = memberReceiveAddressService.find(where((root, query, cb) -> {
            return cb.equal(root.get("memberId"), memberId);
        }), pageable);

        return memberAddress;
    }

    @ApiOperation(value="添加用户收货地址",notes="")
    @RequestMapping(value = "/add", method = RequestMethod.POST, produces = "application/json")
    public MemberReceiveAddress addReceiveAddress(
            @RequestBody MemberReceiveAddress memberReceiveAddress) throws Exception {
        log.info("用户已经登录");

        MemberReceiveAddress memberAddress = memberReceiveAddressService.save(memberReceiveAddress);


        return memberAddress;
    }

    @ApiOperation(value="修改用户收货地址",notes="")
    @RequestMapping(value = "/update", method = RequestMethod.POST, produces = "application/json")
    public MemberReceiveAddress updateReceiveAddress(
            @RequestBody MemberReceiveAddress memberReceiveAddress) throws Exception {
        log.info("修改用户收货地址");

        MemberReceiveAddress memberAddress = memberReceiveAddressService.update(memberReceiveAddress);

        return memberAddress;
    }


}
